<html>
<head><meta charset="utf-8"><title>Conversion between references to repr(C) structs · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html">Conversion between references to repr(C) structs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="172638423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172638423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172638423">(Aug 06 2019 at 23:42)</a>:</h4>
<p>If I have two <code>#[repr(C)]</code> (and <code>Copy</code>) structs <code>Vec4 { x: f32, y: f32, z: f32, w: f32 }</code> and <code>Vec3 { x: f32, y: f32, z: f32 }</code>, is it legal for a <code>&amp;Vec3</code> to alias a <code>&amp;Vec4</code>? e.g. if i transmute <code>&amp;Vec4</code> to <code>&amp;Vec3</code> is that allowed? (I want to know if some of my older code is unsound, or just... dodgy)</p>



<a name="172638734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172638734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172638734">(Aug 06 2019 at 23:49)</a>:</h4>
<p>Dodgy, definitely, not sure if unsound or not. Whether that will work depends entirely on layout compatibility, which is not necessarily compatible for these two structures.</p>



<a name="172639046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172639046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172639046">(Aug 06 2019 at 23:55)</a>:</h4>
<p>the complete declaration is</p>
<div class="codehilite"><pre><span></span>#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct V3 { pub x: f32, pub y: f32, pub z: f32, }

#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[repr(C)]
pub struct V4 { pub x: f32, pub y: f32, pub z: f32, pub w: f32 }
</pre></div>


<p>(with some whitespace removed for brevity) is that enough to determine layout compatibility? or is this something that's still unspecified</p>



<a name="172639123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172639123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172639123">(Aug 06 2019 at 23:56)</a>:</h4>
<p>e.g. there are no other repr's involved, if that's what you meant</p>



<a name="172639302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172639302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172639302">(Aug 07 2019 at 00:00)</a>:</h4>
<p>it also does stuff like convert &amp;[V4] to &amp;[f32] (via from_raw_parts and some pointer casting).  avoiding that would be... harder, unfortunately. this feels less dodgy to me though, but I can't really place why (it might just be that I... want it to be fine <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span>)</p>



<a name="172640032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172640032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172640032">(Aug 07 2019 at 00:16)</a>:</h4>
<p>reinterpretation to a slice would be not fine on any target where f32 was aligned to 64 bits, for example.</p>



<a name="172640035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172640035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172640035">(Aug 07 2019 at 00:16)</a>:</h4>
<p>not that any real target like that comes to mind.</p>



<a name="172640070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172640070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172640070">(Aug 07 2019 at 00:17)</a>:</h4>
<p>Oh wait, never mind, I’m stupid. Repeated note to self: do not think about complicated things when its 3am.</p>



<a name="172640071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172640071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172640071">(Aug 07 2019 at 00:17)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> wouldn't V4 have to be have the same alignment?</p>



<a name="172640083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172640083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172640083">(Aug 07 2019 at 00:17)</a>:</h4>
<p>ah, i'm the same way. sleep is... hard.</p>



<a name="172680247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172680247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172680247">(Aug 07 2019 at 14:00)</a>:</h4>
<blockquote>
<p>If I have two #[repr(C)] (and Copy) structs Vec4 { x: f32, y: f32, z: f32, w: f32 } and Vec3 { x: f32, y: f32, z: f32 }, is it legal for a &amp;Vec3 to alias a &amp;Vec4? e.g. if i transmute &amp;Vec4 to &amp;Vec3 is that allowed?</p>
</blockquote>
<p>I think this is always legal, because the alignment requirement of Vec4 is always equal to or larger than that of Vec3  for all targets we can ever support.</p>



<a name="172680459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172680459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172680459">(Aug 07 2019 at 14:02)</a>:</h4>
<p>Even if this were not the case, you could still do this if you check before that for the particular allocation that you want to alias, that the allocation lives at a multiple of <code>Vec3</code> alignment.</p>



<a name="172680549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion%20between%20references%20to%20repr%28C%29%20structs/near/172680549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Conversion.20between.20references.20to.20repr(C).20structs.html#172680549">(Aug 07 2019 at 14:03)</a>:</h4>
<blockquote>
<p>reinterpretation to a slice would be not fine on any target where f32 was aligned to 64 bits, for example.</p>
</blockquote>
<p>I'm not sure if that's what you meant afterwards, but if f32 was aligned to 64-bit, Vec4 would be aligned to a multiple of that, so I think this should always work.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>